<template>
	<van-address-edit
		v-if="addressInfo"
		:area-list="areaList"
    :address-info="addressInfo"
		show-set-default
		show-search-result
		show-delete
		:search-result="searchResult"
		:area-columns-placeholder="['请选择', '请选择', '请选择']"
		@save="onSave"
		@delete="onDelete" />
</template>

<script setup>
import { areaList } from '@vant/area-data';
import { getCurrentInstance } from 'vue';

const { proxy } = getCurrentInstance();

const id = proxy.$route.query.id;

const addressInfo = ref(null);

// 会员数据
const business = proxy.$cookies.get('business');

async function getData() {
	const res = await proxy.$GET({
		url: '/address/edit',
		params: { busid: business.id, id },
	});

	if (res.code === 0) {
		proxy.$fail(res.msg);
		proxy.$router.back();
  }

	addressInfo.value = {
		isDefault: res.data.status === '1',
		name: res.data.consignee,
		tel: res.data.mobile,
    areaCode: res.data.district || res.data.city || res.data.province,
    addressDetail: res.data.address
	};
}

const searchResult = ref([]);

const onSave = async (info) => {
	const res = await proxy.$POST({
		url: '/address/edit',
		params: { ...info, busid: business.id, id },
	});

	if (res.code === 1) {
		proxy.$success(res.msg);
		// 返回上一页
		proxy.$router.back();
	} else {
		proxy.$fail(res.msg);
	}
};
const onDelete = () => {
	console.log('delete');
};

getData()
</script>

<style scoped></style>
